﻿// Assignment 5
// Written by Amanda Boström
// 2013-10-19


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Assignment_5_Amanda_Boström
{
    public partial class MainForm : Form
    {
        private ContactFiles.ContactManager m_contacts; // contact registry

        public MainForm()
        {
            InitializeComponent();

            // Creates an instance of the contact registry
            m_contacts = new ContactFiles.ContactManager();
            InitializeGUI();
        }
        /// <summary>
        /// Adds countries to the combobox and does other initializations.
        /// </summary>
        private void InitializeGUI()
        {
            cmbCountry.Items.AddRange(Enum.GetNames(typeof(ContactFiles.Countries)));

            cmbCountry.SelectedIndex = (int)ContactFiles.Countries.Sverige;

            txtFirstName.Text = String.Empty;
            txtLastName.Text = String.Empty;
            txtStreet.Text = String.Empty;
            txtCity.Text = String.Empty;
            txtZipCode.Text = String.Empty;
            
        }
        /// <summary>
        /// Checks the input of the textboxes to see there are no empty boxes.
        /// </summary>
        /// <returns></returns>
        private bool ReadInput()
        {
            if (InputUtility.ValidateString(txtFirstName.Text) && InputUtility.ValidateString(txtLastName.Text) &&
                InputUtility.ValidateString(txtStreet.Text) && InputUtility.ValidateString(txtZipCode.Text) && 
                InputUtility.ValidateString(txtCity.Text))
            {
                return true;
            }
            else 
                return false;
        }
        /// <summary>
        /// Reads the input for the first and last name.
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <returns></returns>
        private bool ReadName(out string firstName, out string lastName)
        {
           
                firstName = txtFirstName.Text;
                lastName = txtLastName.Text;
                return true;
            
        }
        /// <summary>
        /// Reads the input of the user to generate an address.
        /// </summary>
        /// <returns></returns>
        private ContactFiles.Address ReadAddress()
        {
            string street, city, zip;
            street = txtStreet.Text;
            city = txtCity.Text;
            zip = txtZipCode.Text;
            ContactFiles.Countries country = (ContactFiles.Countries)cmbCountry.SelectedIndex;
            

            ContactFiles.Address address = new ContactFiles.Address(street, zip, city, country);
            return address;
        }
        /// <summary>
        /// Generates a contact from input values.
        /// </summary>
        /// <returns></returns>
        private ContactFiles.Contact GenerateContact()
        {
            string firstName, lastName;
            ContactFiles.Address adr;
            ReadName(out firstName, out lastName);
            adr = ReadAddress();

            ContactFiles.Contact contact = new ContactFiles.Contact(firstName, lastName, adr);
            return contact;
        }

        /// <summary>
        /// Clears the listbox, calls the method GetContactsInfo of the 
        /// m_contacts-object and using the ListBox's AddRange-method,
        /// add the array to the listbox.
        /// </summary>
        private void UpdateGUI()
        {
            lstContacts.Items.Clear();
            lstContacts.Items.AddRange(m_contacts.GetContactsInfo());
            lblNumOfRecords.Text = lstContacts.Items.Count.ToString();
        }
        /// <summary>
        /// Displays the information of the selected contact in the list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateContactInfoFromRegistry();
        }
        /// <summary>
        /// Updates the input boxes with information for the highlighted
        /// contact in the listbox
        /// </summary>
        private void UpdateContactInfoFromRegistry()
        {
            ContactFiles.Contact contact = m_contacts.GetContact(lstContacts.SelectedIndex);

            cmbCountry.SelectedIndex = (int)contact.AddressData.Country;
            txtFirstName.Text = contact.FirstName;
            txtLastName.Text = contact.LastName;
            txtCity.Text = contact.AddressData.City;
            txtStreet.Text = contact.AddressData.Street;
            txtZipCode.Text = contact.AddressData.ZipCode;

        }
        /// <summary>
        /// Adds a new Contact to the List.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (ReadInput())
            {
                m_contacts.AddContact(GenerateContact());
                UpdateGUI();
                InitializeGUI();
            }
        }
        /// <summary>
        /// Replaces/Changes a contact's information in the registry.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChange_Click(object sender, EventArgs e)
        {
            if (ReadInput())
            {
                m_contacts.DeleteContact(lstContacts.SelectedIndex);
                m_contacts.ChangeContact(GenerateContact(), lstContacts.SelectedIndex);
                UpdateGUI();
                InitializeGUI();
            }

        }
        /// <summary>
        /// Deletes a contact from the registry when prompted by button click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            m_contacts.DeleteContact(lstContacts.SelectedIndex);
            UpdateGUI();
            InitializeGUI();
        }

        private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            ContactFiles.Countries country = (ContactFiles.Countries)cmbCountry.SelectedIndex;
        }
    }
}
